package treetagger;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;


/**
 * Classe de racinisation (stemming) des mots
 * en fran�ais.
 * Modification l�g�re du package SnowBall
 * http://snowball.tartarus.org/download.php
 * @author xtannier
 *
 */
public class FrenchStemmer extends org.tartarus.snowball.ext.frenchStemmer implements Normalizer {
        public static final Charset ENCODING = StandardCharsets.ISO_8859_1;
        
	private static short REPEAT = 1;
	
	private ArrayList<String> stopWords;
	
	public FrenchStemmer() {
		this.stopWords = new ArrayList<String>();
	}
	
	public FrenchStemmer(String stopWordFileName) throws IOException {
		this.stopWords = new ArrayList<String>();
		//lecture du fichier texte	
		InputStream ips=new FileInputStream(stopWordFileName); 
		InputStreamReader ipsr=new InputStreamReader(ips, ENCODING);
		BufferedReader br=new BufferedReader(ipsr);
		String line;
		while ((line=br.readLine())!=null){
			this.stopWords.add(line);
		}
		br.close(); 
	}

	@Override
	public ArrayList<String> normalize(String fileName) {		
		String text = "";
		//lecture du fichier texte
                try{
                    InputStream ips=new FileInputStream(fileName); 
                    InputStreamReader ipsr=new InputStreamReader(ips, ENCODING);
                    BufferedReader br=new BufferedReader(ipsr);
                    String line;
                    while ((line=br.readLine())!=null){
                            text += line + " ";
                    }
                    br.close();
                }
                catch(IOException e){
                    System.out.println(e.getLocalizedMessage());
                }
		
		ArrayList<String> words = (new FrenchTokenizer()).tokenize(text.toLowerCase());
		ArrayList<String> result = new ArrayList<String>();
		for (String word : words) {
			// on ajoute le mot dans la liste s'il n'appartient pas � la liste des mots-cl�s.
			// Id�alement il faudrait utiliser une structure de donn�es plus efficace que la liste,
			// mais ce n'est pas le sujet.
			this.setCurrent(word);
			for (int i = REPEAT; i != 0; i--) {
				this.stem();
			}
			result.add(this.getCurrent());
		}
		return result;
	}

	public static void main(String[] args) {
		String stopWordFileName = "/home/xtannier/Cours/10-11/L3_TIW/td/td2/frenchST.txt";
		String word = "/home/xtannier/tmp/texte.txt";
		try {
			Normalizer stemmer = new FrenchStemmer(stopWordFileName);
			System.out.println(stemmer.normalize(word));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
